const {
  db
} = require('../core/db.js');
const {
  Sequelize,
  Model,
  DataTypes
} = require('sequelize');

const { httpError } = require('../core/httpError.js');

const bcrypt = require('bcryptjs');

class User extends Model {
  static async isAccountPassword (account, password) {

    // const getAccount = await User.findOne({
    //     where: {
    //         nickname: account
    //     }
    // });

    User.findAll({
      where: {
        id: '1'
      }
    }).then(res => {
      console.log('res', res);
    });

    // console.log('getAccount', getAccount);


    // if (!getAccount) {
    //     throw Error('账号不存在');
    // }

    // bcrypt.compareSync(password);

    return {}

  }

  static async getUserByOpenid (openid) {
    const user = await User.findOne({
      where: {
        openid
      }
    });

    return user;
  }

  static async registerByOpenid (openid) {
    console.log('openid', openid);
    return await User.create({
      openid
    });

  }
}

// const User = db.define('user', {
//     'id': {
//         type: Sequelize.STRING(50),
//         primaryKey: true
//     },
//     'nickname': Sequelize.STRING,
//     'email': Sequelize.STRING,
//     'password': {
//         type: Sequelize.STRING,
//         set(val) {
//             const salt = bcrypt.genSaltSync(10);
//             const hash = bcrypt.hashSync(val, salt);
//             this.setDataValue('password', hash.toString());
//         }
//     }
// }, {
//     timestamps: false
// });

User.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  openid: {
    type: DataTypes.STRING
  },
  nickname: DataTypes.STRING,
  email: {
    type: DataTypes.STRING,
    unique: true
  },
  password: DataTypes.STRING
  // openId: {
  //     type: Sequelize.NUMBER(64),
  //     unique: true
  // }
}, {
  sequelize: db,
  tableName: 'users'
});

module.exports = { User };